<?php

namespace backend\controllers;

use backend\models\Baseagency;
use backend\models\Basebonus;
use backend\models\Basemaster;
use backend\models\Basemodule;
use backend\models\Baserole;
use backend\models\Baserolemaster;
use backend\models\Basescalevary;
use backend\models\PlayerCurrencyRecordTbl;
use backend\models\PlayerTbl;
use backend\models\RecordPlayerServiceFeeTbl;
use common\service\PubService;
use yii;

class MastersController extends CommonController
{
    /**
     * @param \yii\base\Action $action
     * @return bool
     * @throws \yii\web\BadRequestHttpException
     */
    public function actionIndex()
    {
        $moduleId = yii::$app->request->get('moduleId');
        $module = Basemodule::findOne(['Id' => $moduleId]);
        if (yii::$app->request->isPost){
            $data = yii::$app->request->post();
            $pageCurrent = $data['pageCurrent'];
            $pageSize = $data['pageSize'];
            $master = new Basemaster();
            $data = $master->getMasterData($pageCurrent, $pageSize);
            return $this->renderJson($data);
        }
        $citem = Basemodule::getMoreRole($this->roleId, $moduleId);
        $data = [
            'module' => $module,
            'citem' => $citem
        ];
        return $this->renderPartial('index',$data);
    }


    public function actionCreate()
    {
        //发布类型
        $rList = Baserole::find()->where(['GroupId'=>1])->asArray()->all();
        if (yii::$app->request->isPost){
            $post = yii::$app->request->post();
            if (empty($post['txt_ID'])){//添加
                $master = new Basemaster();
                $result = $master->insertData($post);
            }else{//修改
                $master = Basemaster::findOne(['MasterId' => $post['txt_ID']]);
                $result = $master->updateData($post);
            }
            return $this->renderJson($result);
        }else{
            $id = yii::$app->request->get('id');
            $masterId = (empty($id)) ? 0 : $id;
            $master = Basemaster::getMasterOne($masterId);
            $data = [
                'rList' => $rList,
                'master' => $master,
            ];
            return $this->renderPartial('create', $data);
        }
    }


    public function actionHandler()
    {
        if ($this->roleId > 2){
            return false;
        }
        $data = yii::$app->request->post();
        $master = new Basemaster();
        $result = $master->deleteData($data);
        return $this->renderJson($result);
    }


    public function actionModifyPassword()
    {
        if (yii::$app->request->isPost){
            $data = yii::$app->request->post();
            $master = new Basemaster();
            $result = $master->changePass($data);
            return $this->renderJson($result);
        }
        return $this->renderPartial('modify-password');
    }


    public function actionUserinfo()
    {
        $uid = yii::$app->user->getId();
        $master = Basemaster::findOne(['MasterId'=>$uid]);
        $roleid = Baserolemaster::findOne(['MasterId'=>$uid])->RoleId;
        $role = Baserole::findOne(['Id'=>$roleid]);
        $address = PubService::addressIp($master->LoginIP);
        $data = [
            'master' => $master,
            'role' => $role,
            'address' => $address,
        ];
        return $this->renderPartial('userinfo', $data);
    }


    public function actionPersonal()
    {
        $uid = yii::$app->user->getId();
        $master = Basemaster::findOne(['MasterId'=>$uid]);
        $playerId = Baseagency::findOne(['AdminId'=>$uid])->PlayerId;
        $playerTbl = PlayerTbl::findOne(['PlayerID'=>$playerId]);
        $agency = PlayerTbl::findOne(['PlayerID'=>$playerTbl->AgentID]);
        $roleid = Baserolemaster::findOne(['MasterId'=>$uid])->RoleId;
        $role = Baserole::findOne(['Id'=>$roleid]);
        $agent = Baseagency::findOne(['AdminId'=>$uid]);
        $sql = "select count(*) from baseagency 
                where Level > {$agent->Level} and ParentId = {$agent->AgentId} and FullDir like '%{$agent->AgentId}%'";
        $levelNum = yii::$app->db->createCommand($sql)->queryScalar();
//        代理的直接玩家数量
//        $sqla = "select count(*) from Player_tbl where AgentID = {$agency->PlayerId}";
//        $playerNum = yii::$app->db->createCommand($sqla)->queryScalar();
//        代理直接玩家和下级代理的所有玩家数量
        $playerIds = Basescalevary::_mergeNoAgentPlayerData($agent->AgentId);
        $agentIds = implode(',', $playerIds);
        if (empty($agentIds)){
            $agentIds = 0;
        }
        $where = " PlayerID in ({$agentIds}) ";
        $sqla = "select count(*) from Player_tbl where $where";
        $playerNum = yii::$app->db->createCommand($sqla)->queryScalar();
        $address = PubService::addressIp($master->LoginIP);
        //本月业绩achievement
        $scalevary = new Basescalevary();
        $achievement = $scalevary->getMonthAchievement($agent->AgentId);
        //本月收益[代理身份本人可提的提成奖金，包含了已经提取的奖金]
        $bonusModel = new Basebonus();
        $bonus = $bonusModel->getThisMonthBonus($playerId);
        //区域代理本月收益
        $areabonus = $bonusModel->getThisMonthAreaBonus($playerId);
        $areaachievement = $scalevary->getMonthAreaAchievement($master->City);
        $data = [
            'playerTbl' => $playerTbl,
            'master' => $master,
            'role' => $role,
            'agent' => $agent,
            'agency' => $agency,
            'levelNum' => $levelNum,
            'playerNum' => $playerNum,
            'address' => $address,
            'bonus' => $bonus,
            'achievement' => $achievement['Amount'],
            'roleName' => $this->roleName,
            'areabonus' => $areabonus,
            'areaachievement' => $areaachievement['Amount'],
        ];
        return $this->renderPartial('personal', $data);
    }


    public function actionOperationLog()
    {
        $moduleId = yii::$app->request->get('moduleId');
        $module = Basemodule::findOne(['Id' => $moduleId]);
        if (yii::$app->request->isPost){
            $post = yii::$app->request->post();
            $pageCurrent = $post['pageCurrent'];
            $pageSize = $post['pageSize'];
            $currency = new PlayerCurrencyRecordTbl();
            $datas = $currency->getOperationLogList($post, $pageCurrent, $pageSize);
            return $this->renderJson($datas);
        }
        $citem = Basemodule::getMoreRole($this->roleId, $moduleId);
        $data = [
            'module' => $module,
            'citem' => $citem
        ];
        return $this->renderPartial('operation-log', $data);
    }


    public function actionTaxstatistics()
    {
        $moduleId = yii::$app->request->get('moduleId');
        $module = Basemodule::findOne(['Id' => $moduleId]);
        if (yii::$app->request->isPost){
            $post = yii::$app->request->post();
            $pageCurrent = $post['pageCurrent'];
            $pageSize = $post['pageSize'];
            $serviceFee = new RecordPlayerServiceFeeTbl();
            $datas = $serviceFee->getLossServiceFee($post, $pageCurrent, $pageSize);
            return $this->renderJson($datas);
        }
        $citem = Basemodule::getMoreRole($this->roleId, $moduleId);
        $data = [
            'module' => $module,
            'citem' => $citem
        ];
        return $this->renderPartial('tax-statistics', $data);
    }


    public function actionTongji()
    {
        if (yii::$app->request->isPost){
            $post = yii::$app->request->post();
            $rewardGoldcoin = new RecordPlayerServiceFeeTbl();
            $data = $rewardGoldcoin->tongji($post);
            return $this->renderJson($data);
        }
        return $this->renderJson(['statusCode' => 300, 'message'=>'非法操作']);
    }


}
